<template>
	<view class="fun-flex-col page">
		<view class="page-bg">
			<view class="fun-flex-row fun-justify-between fun-items-start group">
				<view class="left fun-flex-col">
					<view>
						<view class="font_2">Available Balance</view>
						<view class="text_4">${{walletInfo.balance}}</view>
					</view>
					
					<view class="withdrawals">
						<view class="font_3 text_6">In Withdrawals</view>
						<view class="text_7">${{walletInfo.withdrawaling}}</view>
					</view>
				</view>
				
				<view class="right">
					<view class="fun-flex-row fun-justify-between fun-items-center section_2" @click="toTopUp">
						<image class="image_5" src="@/static/account/top_up.svg" />
						<text class="text_3">Top up</text>
					</view>
					
					<view class="fun-flex-row fun-justify-center fun-items-center section_3" @click="toWithdraw">
						<image class="image_5" src="@/static/account/withdraw.svg" />
						<text class="font_3 text_5">Withdraw</text>
					</view>
				</view>
			</view>
		</view>
		
		
		<view class="fun-flex-col group_2">
			<view class="fun-flex-row fun-justify-between fun-items-center section_4">
				<text class="font_4 text_8">Top Up Activities</text>
				<view class="fun-flex-row fun-items-center section_5" @click="allTopUp">
					<text class="font_5 text_9">ALL</text>
					<image class="fun-shrink-0 image_6 fun-ml-8" src="@/static/common/more.png" />
				</view>
			</view>
			
			<view class="fun-flex-col">
				<view class="fun-flex-col list-item" v-for="item in rechargeList" :key="item.id" @click="toDetail(item,'recharge')">
					<view class="fun-flex-row fun-justify-between fun-items-baseline fun-self-stretch">
						<text class="font font-title">Top-Up to my account</text>
						<text class="money to-up">+${{item.priceAmount}}</text>
					</view>
					<text class="fun-self-start font_7">Transaction Fee:${{roundToDecimal(item.priceAmount * item.serviceCharge/100)}}</text>
					<view class="fun-flex-row fun-justify-between fun-items-end fun-self-stretch group_3">
						<text class="font_8 text_10">Order No. :{{item.orderNo}}</text>
						
						<view class="status progress" v-if='item.status == 1 || item.status == 2 || item.status == 3'>{{item.status | rechargeStatus}}</view>
						<view class="status completed" v-if='item.status == 4'>{{item.status | rechargeStatus}}</view>
						<view class="status failed" v-if='item.status == 5 || item.status == 6'>{{item.status | rechargeStatus}}</view>
					</view>
				</view>
			</view>
		</view>
		
		<view class="fun-flex-col group_2">
			<view class="fun-flex-row fun-justify-between fun-items-center section_4">
				<text class="font_4 text_8">Withdraw Activities </text>
				<view class="fun-flex-row fun-items-center section_5" @click="allWithdraw">
					<text class="font_5 text_9">ALL</text>
					<image class="fun-shrink-0 image_6 fun-ml-8" src="@/static/common/more.png" />
				</view>
			</view>
			
			<view class="fun-flex-col">
				<view class="fun-flex-col list-item" v-for="item in withdrawList" :key="item.id" @click="toDetail(item,'withdraw')">
					<view class="fun-flex-row fun-justify-between fun-items-baseline fun-self-stretch">
						<text class="font font-title">Withdraw from my account</text>
						<text class="money withdraw">-${{item.putAmount}}</text>
					</view>
					<text class="fun-self-start font_7">Transaction Fee:${{item.serviceAmount}}</text>
					<view class="fun-flex-row fun-justify-between fun-items-end fun-self-stretch group_3">
						<text class="font_8 text_10">Order No. :{{item.orderNo}}</text>
						<view class="status progress" v-if='item.status == 1 || item.status == 5'>{{item.status | withdrawStatus}}</view>
						<view class="status completed" v-if='item.status == 6'>{{item.status | withdrawStatus}}</view>
						<view class="status failed" v-if='item.status == 7 || item.status == 8'>{{item.status | withdrawStatus}}</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {roundToDecimal} from "@/common/js/func.js"
	import {getTimeZone} from "@/common/js/func.js"
	import {wallet,rechargeOrderList,withdrawOrderList} from "@/api/account.js"
	export default {
		data() {
			return {
				queryData:{
					page:1,
					limit:3,
					status:0,
					timeZone:8
				},
				rechargeList:[],
				withdrawList:[],
				walletInfo:{},
				roundToDecimal
			};
		},
		
		onLoad() {
			this.queryData.timeZone = getTimeZone() || 8;
			this.getRechargeOrderlist()
			this.getWithdrawOrderlist()
			this.getWalletInfo()
			uni.$on('update', this.getRechargeOrderlist)
		},

		methods: {
			getRechargeOrderlist(){
				rechargeOrderList(this.queryData).then(res => {
					if(res.code == 0){
						this.rechargeList = res.data || [];
					}
					else{
						this.UTILS.logStr(res.msg || "Failed to retrieve data")
					}
				})
			},
			
			getWithdrawOrderlist(){
				withdrawOrderList(this.queryData).then(res => {
					if(res.code == 0){
						this.withdrawList = res.data || [];
					}
					else{
						this.UTILS.logStr(res.msg || "Failed to retrieve data")
					}
				})
			},
			
			getWalletInfo(){
				wallet().then(res => {
					if(res.code == 0){
						this.walletInfo = res.data || {};
					}
					else{
						this.UTILS.logStr(res.msg || "Failed to retrieve data")
					}
				})
			},
			
			toTopUp(){
				uni.navigateTo({url:'/pages/account/topUp'})
			},
			
			toWithdraw(){
				uni.navigateTo({url:'/pages/account/withdraw'})
			},
			
			toDetail(item,type){
				if(type == "withdraw"){
					uni.navigateTo({url:'/pages/account/withdrawDetail?orderId='+encodeURIComponent(item.id)})
				}else{
					uni.navigateTo({url:'/pages/account/topUpDetail?orderId='+encodeURIComponent(item.id)})
				}
			},
			
			allWithdraw(){
				uni.navigateTo({url:'/pages/account/withdrawList'})
			},
			
			allTopUp(){
				uni.navigateTo({url:'/pages/account/topUpList'})
			}
		},
		
		onUnload(){
			uni.$off('update')
		}
	};
</script>

<style scoped lang="scss">
	.ml-9 {
		margin-left: 18rpx;
	}
	.page {
		padding-bottom: 32rpx;
		background-color: #ffffff;
		width: 100%;
		overflow-y: auto;
		overflow-x: hidden;
		height: 100%;
		.page-bg{
			padding: 32rpx;
		}
		.group {
			width: 100%;
			padding: 32rpx;
			background: url("@/static/home/invest_bg.png") no-repeat center;
			background-size: 100% 100%;
			.font_2 {
				font-size: 32rpx;
				font-family: SF Pro Display;
				font-weight: 600;
				color: #000000;
			}
			.withdrawals{
				margin-top: 40rpx;
			}
			.text_4 {
				color: #1a7b99;
				font-size: 56rpx;
				line-height: 72rpx;
				margin-top: 4rpx;
			}
			.text_7 {
				color: #7f7f7f;
				font-size: 36rpx;
				font-weight: 600;
			}
			.text_6 {
				color: #979797;
			}
			.section_3 {
				padding: 16rpx 20rpx;
				background-color: #1a7b99;
				border-radius: 8rpx;
				margin-top: 32rpx;
				.text_5 {
					color: #ffffff;
					margin-left: 16rpx;
				}
			}
			.section_2 {
				padding: 16rpx 40rpx 16rpx 24rpx;
				background-color: #1a7b99;
				border-radius: 8rpx;
				.text_3 {
					color: #ffffff;
					font-size: 28rpx;
				}
			}
			.image_5 {
				width: 30rpx;
				height: 30rpx;
			}
		}

		.group_2 {
			.section_4 {
				padding: 24rpx 40rpx;
				background-color: #f7f7f7;
				.font_4 {
					font-size: 28rpx;
					font-family: SF Pro Display;
					color: #737580;
				}

				.text_8 {
					font-size: 30rpx;
				}

				.section_5 {
					padding: 6rpx 28rpx 6rpx 40rpx;
					background-color: #ffffff80;
					border-radius: 24rpx;
					border: solid 2rpx #b0b1bf80;
					.image_6 {
						width: 30rpx;
						height: 28rpx;
					}
				}
			}

			.list-item {
				padding: 32rpx 32rpx 0 32rpx;
				.font-title{
					font-weight: bold;
				}
				.money {
					font-size: 28rpx;
					color: #51844d;
					font-weight: bold;
				}
				.top-up{
					color: #51844d;
				}
				.withdraw{
					color: #CA5456;
				}

				.font_7 {
					font-size: 24rpx;
					font-family: SF Pro Display;
					color: #999999;
					margin-top: 20rpx;
					margin-bottom: 10rpx;
				}
				.group_3 {
					padding-bottom: 32rpx;
					border-bottom: 2rpx solid rgba(176,177,191,0.14);
					.status {
						width: 188rpx;
						height: 40rpx;
						text-align: center;
						line-height: 40rpx;
						border-radius: 4rpx;
						font-weight: 500;
					}
					.progress{
						color: #8C7F44;
						background: #FDF0BA;
					}
					
					.completed{
						color: #51844D;
						background: #E9FAF0;
					}
					
					.failed{
						color: #CA5456;
						background: rgba(255,146,165,0.3);
					}
				}
			}
		}

		.font {
			font-size: 32rpx;
			font-family: SF Pro Display;
			color: #000000;
		}

		.font_3 {
			font-size: 28rpx;
			font-family: SF Pro Display;
			color: #8c7f44;
		}
		.font_5 {
			font-size: 24rpx;
			font-family: SF Pro Display;
			color: #7f7f7f;
		}

		.font_8 {
			font-size: 24rpx;
			font-family: SF Pro Display;
			color: #000000;
		}

		.text_10 {
			opacity: 0.5;
		}
	}
</style>